/**  
 *  Copyright 2009, Shanghai Huateng Co., Ltd.  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SHANGHAI HUATENG CO.,
 *  LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIRD
 *  PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF SHANGHAI HUATENG CO., LTD.   
 *
 */
package com.ht.common.util;

import org.apache.commons.codec.binary.Base64;

/** 
 *  功能描述：码字转换
 *  
 *	$Id: $
 *
 *  Edit History:
 *    
 */
public class Coding {
	
	
	/** The Constant str. */
	public final static String STR = "0123456789ABCDEF";
	
	/**
	 * 字符串转换成十六进制值.
	 * 
	 * @param bin String 我们看到的要转换成十六进制的字符串
	 * 
	 * @return the string
	 */
    public static String bin2hex(String bin) {
        char[] digital = STR.toCharArray();
        StringBuffer sb = new StringBuffer("");
        byte[] bs = bin.getBytes();
        int bit;
        final int j = 4;
        final int k = 0x0f0;
        final int l = 0x0f;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & k) >> j;
            sb.append(digital[bit]);
            bit = bs[i] & l;
            sb.append(digital[bit]);
        }
        return sb.toString();
    }

    /**
     * 十六进制转换字符串.
     * 
     * @param hex String 十六进制
     * 
     * @return String 转换后的字符串
     */
    public static String hex2bin(String hex) {
        
        char[] hex2char = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];
        int temp;
        final int k = 16;
        final int j = 0xff;
        for (int i = 0; i < bytes.length; i++) {
            temp = STR.indexOf(hex2char[2 * i]) * k;
            temp += STR.indexOf(hex2char[2 * i + 1]);
            bytes[i] = (byte) (temp & j);
        }
        return new String(bytes);
    }

    /**
     * java字节码转字符串.
     * 
     * @param b the b
     * 
     * @return the string
     */
    public static String byte2hex(byte[] b) {
    	//一个字节的数，

        // 转成16进制字符串

        String hs = "";
        String tmp = "";
        final int i = 0XFF;
        for (int n = 0; n < b.length; n++) {
            //整数转成十六进制表示
        	
            tmp = (java.lang.Integer.toHexString(b[n] & i));
            if (tmp.length() == 1) {
                hs = hs + "0" + tmp;
            } else {
                hs = hs + tmp;
            }
        }
        tmp = null;
        return hs.toUpperCase(); 
        //转成大写

    }

    /**
     * 字符串转java字节码.
     * 
     * @param b the b
     * 
     * @return the byte[]
     */
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0) {
            throw new IllegalArgumentException("长度不是偶数");
        }
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个进制字节
            final int radix = 16;
            b2[n / 2] = (byte) Integer.parseInt(item, radix);
        }
        //b = null;
        return b2;
    }

    /**
     * 取得整数值二进制位的值
     * 
     * @param i
     * @param bitset
     * @param oprLenth
     * @return
     */
    public static String getBitValue(Integer data,int bitset,int oprLenth)
    {
    	String str = Integer.toBinaryString(data);
    	if(str.length()<oprLenth)
    	{
    		int count = oprLenth - str.length();
    		for(int i = 0; i< count; i++)
    		{
    			str = "0" + str;
    		}
    	}
    	return str.substring(bitset,bitset + 1);
    }
    
    public static String base64Encoding(String str)
    {
    	Base64 base64 = new Base64();
		byte[] enbytes = null;
		String encodeStr = null;
		enbytes = base64.encode(str.getBytes());
		encodeStr = new String(enbytes);
		encodeStr=encodeStr.replaceAll("\\+", "%2B");
    	return encodeStr;
    }
    
    public static String base64Decode(String str)
    {
    	str=str.replaceAll("%2B", "+");
    	Base64 base64 = new Base64();
    	byte[] debytes = null;
    	String decodeStr = null;
    	debytes = base64.decode(str.getBytes());
    	decodeStr = new String(debytes);

    	return decodeStr;
    }
    
    /**
     * The main method.
     * 
     * @param args the arguments
     */
    public static void main(String[] args) {
    	
    }

}
